package com.aq.appserver.server.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.aq.appserver.common.util.DateUtil;
import com.aq.appserver.server.theme.bean.ThemeBeanWeb;
import com.aq.appserver.server.theme.entity.Theme;
import com.aq.appserver.server.theme.service.ThemeService;

@Component
public class TaskJob {
	private Logger logger = Logger.getLogger(TaskJob.class);

	@Autowired
	private ThemeService themeService;

	@Scheduled(cron = "0 0/5 * * * ?")
	// @Scheduled(cron = "0 15 2 * * ?")
	public void themeTopTimeTask() {
		Iterator<Theme> themeList = themeService.queryWebThemeList().iterator();
		int count = 0;
		Theme tempTheme = null;
		if (themeList != null) {
			while (themeList.hasNext()) {
				// 判断置顶时间是否超过24小时
				tempTheme = themeList.next();
				if (count > 2) {
					break;
				}
				String topTime = tempTheme.getTopTime();
				SimpleDateFormat sdf = new java.text.SimpleDateFormat(
						"yyyy-M-d HH:mm:ss");
				try {
					if (!StringUtils.isEmpty(topTime)) {
						boolean result = DateUtil.jisuan(topTime,
								sdf.format(new Date()));
						if (!result) {
							ThemeBeanWeb themeBeanWeb = new ThemeBeanWeb();
							themeBeanWeb.setId(tempTheme.getId());
							themeService.cancerTopWebTheme(themeBeanWeb);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				count++;
			}
		}
	}
}
